import timeit
import re


# M=1000 D=500 C=100 L=50 X=10 V=5 I=1
def main():
    class Solution:
        def romanToInt(self, s):
            """
            :type s: str
            :rtype: int
            """
            s_list = list(s)
            s_list.reverse()
            i = 0
            num = 0
            while s_list != []:
                if s_list[i] == "I":
                    num += 1
                    s_list = s_list[1:]
                    break
                if s_list[i] == "V":
                    if len(s_list)>1 and s_list[i + 1] == "I":
                        num += 4
                        s_list = s_list[2:]
                    else:
                        num += 5
                        s_list = s_list[1:]
                    continue
                if s_list[i] == "X":
                    if len(s_list)>1 and s_list[i + 1] == "I":
                        num += 9
                        s_list = s_list[2:]
                    else:
                        num += 10
                        s_list = s_list[1:]
                    continue
                if s_list[i] == "L":
                    if len(s_list)>1 and s_list[i + 1] == "X":
                        num += 40
                        s_list = s_list[2:]
                    else:
                        num += 50
                        s_list = s_list[1:]
                    continue
                if s_list[i] == "C":
                    if len(s_list)>1 and s_list[i + 1] == "X":
                        num += 90
                        s_list = s_list[2:]
                    else:
                        num += 100
                        s_list = s_list[1:]
                    continue
                if s_list[i] == "D":
                    if len(s_list)>1 and s_list[i + 1] == "C":
                        num += 400
                        s_list = s_list[2:]
                    else:
                        num += 500
                        s_list = s_list[1:]
                    continue
                if s_list[i] == "M":
                    if len(s_list)>1 and s_list[i + 1] == "C":
                        num += 900
                        s_list = s_list[2:]
                    else:
                        num += 1000
                        s_list = s_list[1:]
                    continue
            return num

    s = Solution()
    print(s.romanToInt("DCXXI"))


if __name__ == '__main__':
    t1 = timeit.timeit(stmt=main, number=1)
    print(t1)
